﻿using UnityEngine;
using System.Collections.Generic;

public class UIManager : SingletonMono<UIManager>
{
    Canvas _root;
    Canvas RootCanvas
    {
        get
        {
            if (_root == null)
            {
                var canvasGo = GameObject.Find("Canvas");
                if (canvasGo == null)
                {
                    var canvas_prefab = Resources.Load("UI/Canvas");
                    canvasGo = (GameObject)Instantiate(canvas_prefab);
                    DontDestroyOnLoad(canvasGo);

                }
                _root = canvasGo.GetComponent<Canvas>();
            }
            return _root;
        }
    }
    public List<BaseUI> allUI_List = new List<BaseUI>();//所有UI
    public List<BaseUI> allUI_statck = new List<BaseUI>(); //堆栈的排列

    private int ui_seqId = 0;

    private string UIPath = "UI/";

    public void Init()
    {
        var canvas = RootCanvas;
    }

    public BaseUI Open(UIConst uiType, UIOpenType openType = UIOpenType.HideLast)
    {
        var curUI = allUI_List.Find(x => x.UI_Type == uiType);
        if (curUI != null)
        {
            curUI.gameObject.SetActive(true);
            var ui_Index = allUI_statck.FindIndex(x => x.UI_Type == uiType);
            if(ui_Index >= 0) //置顶
            {
                allUI_statck.RemoveAt(ui_Index);
                //allUI_statck.Insert(0, curUI);
            }

        }
        else
        {
            string path = UIPath + uiType.ToString();
            var ui_Prefab = Resources.Load(path);
            if (ui_Prefab == null)
            {
                Log.ErrorFormat("UI_Prefab {0} is Null", "", uiType.ToString());
                return null;
            }
            var ui_go = Instantiate(ui_Prefab, RootCanvas.transform) as GameObject;
            curUI = ui_go.GetComponent<BaseUI>();

            curUI.transform.localPosition = Vector3.zero;
            curUI.transform.localScale = Vector3.one;

            curUI.ui_Id = ui_seqId++;
            allUI_List.Add(curUI);
        }
       

        if (openType == UIOpenType.HideLast)
        {
            HideLast(false);
           
        }
        if(openType == UIOpenType.CloseLast)
        {
            HideLast(true);
        }

        allUI_statck.Insert(0, curUI);
        

        return curUI;
    }

    //public void Close(UIConst uiType)
    //{
    //    var res = allUI_List.FindAll(x => x.UI_Type == uiType);

    //    foreach (var ui in res)
    //    {
    //        ui.OnClose();
    //        Destroy(ui.gameObject);
    //    }
    //    allUI_List.RemoveAll(x => x.UI_Type == uiType);

    //    if (allUI_statck.Count > 0)
    //        allUI_statck[0].gameObject.SetActive(true);
    //}

   public void Close(UIConst uiType,int ui_id = -1)
    {

        //关闭指定的UI
        if(ui_id >= 0)
        {
            var index = allUI_statck.FindIndex(x => x.ui_Id == ui_id);
            if (index >= 0)
            {
                var ui = allUI_statck[index];
                Destroy(ui.gameObject);
                allUI_statck.RemoveAt(index);
            }

            allUI_List.RemoveAll(x => x.ui_Id == ui_id);

            return;
        }

        foreach( var ui in allUI_statck.FindAll(x => x.UI_Type == uiType))
        {
            Destroy(ui.gameObject);
        }

        allUI_List.RemoveAll(x => x.UI_Type == uiType);
        allUI_statck.RemoveAll(x => x.UI_Type == uiType);
    }
    public void Close()
    {
        if (allUI_statck.Count > 0)
        {
            var topUI = allUI_statck[0];
            Destroy(topUI.gameObject);
            allUI_statck.RemoveAt(0);
            RemoveOnAll(topUI.UI_Type,topUI.ui_Id);
            if (allUI_statck.Count > 0)
            {
                topUI = allUI_statck[0];
                topUI.gameObject.SetActive(true);
            }
        }

    }

    public void CloseAll()
    {
        for(int i = allUI_List.Count - 1;i>= 0;i--)
        {
            Close(allUI_List[i].UI_Type, allUI_List[i].ui_Id);
        }
    }
    private void RemoveOnAll(UIConst uiType,int ui_id = -1)
    {
        var ui_list = allUI_List.FindAll(x => x.UI_Type == uiType);
        if(ui_list.Count > 0)
        {
            if(ui_id >= 0)
            {
                allUI_List.RemoveAll(x => x.UI_Type == uiType && x.ui_Id == ui_id);
            }
            else
            {
                allUI_List.RemoveAll(x => x.UI_Type == uiType);
            }
            
        }
    }
    public void HideLast(bool isClose)
    {
        if (allUI_statck.Count <= 0)
            return;

        var baseUI = allUI_statck[0];
        if (isClose)
        {
            Close(baseUI.UI_Type, baseUI.ui_Id);
        }
        else
            baseUI.gameObject.SetActive(false);
    }
}
public enum UIOpenType
{
    Normal, //正常开启，不进入堆栈管理
    CloseLast,// 关闭上一个，移除堆栈
    HideLast //隐藏上一个，进入堆栈
}